<?php

namespace app\admin\controller\api;

use app\common\model\HrCode;
use think\Validate;

/**
 * 激活码
 * @internal
 */
class Code extends Api
{
    protected $noNeedLogin = ['lists', 'activate', 'checkStatus'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 激活码列表
     */
    public function index()
    {
        $admin_id = $this->_user['id'];

        $list = HrCode::alias('code')
            ->join('__SCHOOL__ school', 'school.id = code.school_id', 'left')
            ->where('code.admin_id', $admin_id)
            ->field('code.*,school.name school_name')
            ->select();

        $this->success('', $list);
    }

    /**
     * 绑定学校
     *
     * @User ZhenZiHan
     * @Date 2020/2/27
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bindSchool()
    {
        $code_id = $this->request->request('code_id');// id
        $school_id = $this->request->request('school_id');// 学校

        if (!$code_id || !$school_id) {
            $this->error('参数有误');
        }

        $admin_id = $this->_user['id'];
        $hr_code = HrCode::where('id', $code_id)
            ->where('admin_id', $admin_id)
            ->find();

        if (!$hr_code) {
            $this->error('数据不存在');
        }

        if ($hr_code->school_id > 0) {
            $this->error('已经绑定过学校了，不能重复绑定');
        }

        $school = \app\common\model\School::where('id', $school_id)->count();
        if (!$school) {
            $this->error('学校不存在');
        }

        $hr_code->school_id = $school_id;
        $flag = $hr_code->save();

        if (!$flag) {
            $this->error('绑定失败');
        }

        $this->success('绑定成功');
    }

    /**
     * 解绑
     *
     * @User ZhenZiHan
     * @Date 2020/2/27
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function unbindSchool()
    {
        $code_id = $this->request->request('code_id');// id

        if (!$code_id) {
            $this->error('参数有误');
        }

        $admin_id = $this->_user['id'];
        $hr_code = HrCode::where('id', $code_id)
            ->where('admin_id', $admin_id)
            ->find();

        if (!$hr_code) {
            $this->error('数据不存在');
        }

        if (!$hr_code->school_id) {
            $this->error('解绑失败，该设备没有绑定学校');
        }

        $hr_code->school_id = 0;
        $flag = $hr_code->save();

        if ($flag) {
            $this->success('解绑成功');
        }
        $this->error('解绑失败');
    }

    /**
     * 获取激活码信息
     *
     * @User ZhenZiHan
     * @Date 2020/2/27
     *
     */
    public function lists()
    {
        $codes = $this->request->request('codes/a');

        if (!$codes || !is_array($codes)) {
            $this->error('参数有误');
        }

        $list = HrCode::alias('code')
            ->join('__SCHOOL__ school', 'school.id = code.school_id', 'left')
            ->whereIn('code.code', $codes)
            ->column('code.*,school.name school_name', 'code.id');

        $this->success('', $list);
    }

    /**
     * 激活
     */
    public function activate()
    {
        $code = $this->request->request('code'); // 激活码
        $device = $this->request->request('device');// 设备
        $brand = $this->request->request('brand');// 品牌

        $rule = [
            'code' => 'require',
            'device' => 'require',
            'brand' => 'require',
        ];

        $validate = new Validate($rule);
        $result = $validate->check(compact('code', 'device', 'brand'));
        if (!$result) {
            $this->error($validate->getError());
        }

        $hr_code = HrCode::where('code', $code)->find();
        if (!$hr_code) {
            $this->error('激活码错误');
        }

        if ($hr_code->status != HrCode::NOT_ACTIVATE) {
            $this->error('激活码已被使用');
        }

        $hr_code->device = $device;
        $hr_code->brand = $brand;
        $hr_code->activated_at = time();
        $hr_code->status = HrCode::ACTIVATE;

        $flag = $hr_code->save();

        if ($flag) {
            $this->success();
        } else {
            $this->error('激活失败');
        }

    }

    /**
     * 检测激活码时候激活
     *
     * @User ZhenZiHan
     * @Date 2020/2/27
     *
     */
    public function checkStatus()
    {
        $code = $this->request->request('code');
        if (!$code) {
            $this->error('参数有误');
        }

        $hr_code = HrCode::where('code', $code)
            ->find();

        if (!$hr_code) {
            $this->error('数据不存在');
        }

        $this->success('', $hr_code->status);
    }

    /**
     * 修改
     *
     * @User ZhenZiHan
     * @Date 2020/2/27
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $code = $this->request->request('code'); // 激活码
        $device = $this->request->request('device');// 设备
        $brand = $this->request->request('brand');// 品牌

        if (!$code || (!$device && !$brand)) {
            $this->error('参数有误');
        }

        $admin_id = $this->_user['id'];
        $hr_code = HrCode::where('code', $code)
            ->where('admin_id', $admin_id)
            ->find();

        if (!$hr_code) {
            $this->error('数据不存在');
        }

        if ($device) {
            $hr_code->device = $device;
        }

        if ($brand) {
            $hr_code->brand = $brand;
        }

        $flag = $hr_code->save();

        if ($flag) {
            $this->success('修改成功');
        }

        $this->error('修改失败');

    }

}